package com.itheima.service.impl;

import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.SetMealMapper;
import com.itheima.pojo.entity.CheckGroup;
import com.itheima.pojo.entity.CheckItem;
import com.itheima.pojo.entity.Setmeal;
import com.itheima.service.SetMealService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @className SetMealServiceImpl
 */
@Service
@RequiredArgsConstructor
public class SetMealServiceImpl implements SetMealService {

    private final SetMealMapper setMealMapper;
    private final CheckGroupMapper checkGroupMapper;

    /**
     * 查询所有套餐
     * @param
     * @return
     */
    public List<Setmeal> getSetMealList() {
        return setMealMapper.selectSetMealList();
    }

    /**
     * 根据套餐id查询套餐
     * @param setMealId 套餐id
     * @return
     */
    public Setmeal getSetMealById(Integer setMealId) {

        //写法2：
        return Optional.ofNullable(setMealMapper.selectSetMealById(setMealId))
                .map(setMeal -> {
                    // 查询并设置检查组
                    Optional.ofNullable(checkGroupMapper.selectCheckGroupIdsBySetmealId(setMeal.getId()))
                            .filter(ids -> !ids.isEmpty())
                            .ifPresent(checkGroupIds -> {
                                List<CheckGroup> checkGroups = checkGroupMapper.selectCheckGroupsByIds(checkGroupIds);
                                setMeal.setCheckGroups(checkGroups);

                                // 为每个检查组查询并设置检查项
                                checkGroups.forEach(checkGroup -> {
                                    Optional.ofNullable(checkGroupMapper.selectCheckItemIdsByCheckGroupId(checkGroup.getId()))
                                            .filter(itemIds -> !itemIds.isEmpty())
                                            .ifPresent(itemIds -> {
                                                List<CheckItem> checkItems = checkGroupMapper.selectCheckItemsByIds(itemIds);
                                                checkGroup.setCheckItems(checkItems);
                                            });
                                });
                            });
                    return setMeal;
                }).orElse(null);

         /*// 写法1：
        //先查询套餐
        Setmeal setMeal = setMealMapper.selectSetMealById(setMealId);
        //再查询检查组id集合
        List<Integer> checkGroupIds = checkGroupMapper.selectCheckGroupIdsBySetmealId(setMealId);
        //根据检查组id集合查询检查组集合
        if (checkGroupIds != null && checkGroupIds.size() > 0) {
            List<CheckGroup> checkGroupList = checkGroupMapper.selectCheckGroupsByIds(checkGroupIds);
            setMeal.setCheckGroups(checkGroupList);
            //遍历检查组集合且根据检查组id查询检查项id集合
            for (CheckGroup checkGroup : checkGroupList) {
                List<Integer> checkItemIds = checkGroupMapper.selectCheckItemIdsByCheckGroupId(checkGroup.getId());
                //根据检查项id集合查询检查项集合
                if (checkItemIds != null && checkItemIds.size() > 0) {
                    List<CheckItem> checkItemList = checkGroupMapper.selectCheckItemsByIds(checkItemIds);
                    checkGroup.setCheckItems(checkItemList);
                }
            }
        }
        return setMeal;*/
    }
}
